InΒ [Β ]:
# Installing roboflow library
!pip install roboflow
InΒ [1]:
import torch
print(torch.cuda.is_available()) # Returns True if CUDA is available, else False
InΒ [Β ]:
from roboflow import Roboflow
rf = Roboflow(api_key="Kl0zacUMgzHFXKRFAFtJ")
project = rf.workspace("project-saocp").project("pothole-2-mhkce")
version = project.version(3)
dataset = version.download("yolov11")
# Downloading the dataset from roboflow universe using api key
16 August 2025
InΒ [Β ]:
from roboflow import Roboflow
rf = Roboflow(api_key="Kl0zacUMgzHFXKRFAFtJ")
project = rf.workspace("smartathon").project("new-pothole-detection")
version = project.version(2)
dataset = version.download("yolov11")
# Downloading the dataset from roboflow universe using api key
InΒ [3]:
!pip install ultralytics
InΒ [Β ]:
!pip install opencv-python
# !pip install os
17 August 2025
InΒ [19]:
import cv2
import os
# Using opencv and yolo dimensions for annotation
# Paths
images_folder = r"D:\ASTC project\New-pothole-detection-2\train\images"
labels_folder = r"D:\ASTC project\New-pothole-detection-2\train\labels"
output_folder = "annotated_images"
os.makedirs(output_folder, exist_ok=True)
# Loop through images
for img_file in os.listdir(images_folder):
if not img_file.lower().endswith((".jpg", ".png", ".jpeg")):
continue
img_path = os.path.join(images_folder, img_file)
label_path = os.path.join(labels_folder, os.path.splitext(img_file)[0] + ".txt")
img = cv2.imread(img_path)
if img is None:
print(f"{img_file} not found")
continue
h, w, _ = img.shape
with open(label_path, "r") as f:
for line in f:
parts = line.strip().split()
if len(parts) > 5:
continue
class_id, x_center, y_center, bw, bh = map(float, line.split())
class_id = int(class_id)
xmin = int((x_center - bw / 2) * w)
xmax = int((x_center + bw / 2) * w)
ymin = int((y_center - bh / 2) * h)
ymax = int((y_center + bh / 2) * h)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
cv2.putText(img,"",(xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# saving each file into output folder
out_path = os.path.join(output_folder, img_file)
cv2.imwrite(out_path, img)
18 August 2025
InΒ [1]:
import cv2
import os
# Using opencv and yolo dimensions for annotation
# Paths
images_folder = r"D:\ASTC project\Pothole-2-3\train\images"
labels_folder = r"D:\ASTC project\Pothole-2-3\train\labels"
output_folder = "annotated_images_Pothole-2-3"
os.makedirs(output_folder, exist_ok=True)
# Loop through images
for img_file in os.listdir(images_folder):
if not img_file.lower().endswith((".jpg", ".png", ".jpeg")):
continue
img_path = os.path.join(images_folder, img_file)
label_path = os.path.join(labels_folder, os.path.splitext(img_file)[0] + ".txt")
img = cv2.imread(img_path)
if img is None:
print(f"{img_file} not found")
continue
h, w, _ = img.shape
with open(label_path, "r") as f:
for line in f:
parts = line.strip().split()
if len(parts) > 5:
continue
class_id, x_center, y_center, bw, bh = map(float, line.split())
class_id = int(class_id)
xmin = int((x_center - bw / 2) * w)
xmax = int((x_center + bw / 2) * w)
ymin = int((y_center - bh / 2) * h)
ymax = int((y_center + bh / 2) * h)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
cv2.putText(img,"",(xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# saving each file into output folder
out_path = os.path.join(output_folder, img_file)
cv2.imwrite(out_path, img)
using yolov11 model for trainingΒΆ
InΒ [2]:
!nvidia-smi
InΒ [Β ]:
# Setting up cuda and environment and installing dependencies for model training
InΒ [1]:
import torch
print("CUDA available:", torch.cuda.is_available())
print("PyTorch CUDA version:", torch.version.cuda)
print("Device count:", torch.cuda.device_count())
InΒ [13]:
from ultralytics import YOLO
import os
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
model = YOLO("yolo11n.pt")
model.train(data="D:\ASTC project\Pothole-2-3\data.yaml",epochs = 40 ,imgsz= 640, batch = 16,name = "yolon_final")
InΒ [3]:
from ultralytics import YOLO
inference_nano_model= YOLO(r"D:\ASTC project\runs\detect\yolon_final\weights\best.pt")
InΒ [18]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_nano_model.predict(image_path ,save_dir = "D:\ASTC project\nano_model_1_POthole2-3",conf = 0.25 ,save=True,device= "cuda")
InΒ [19]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
19 August 2025
InΒ [20]:
import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))
InΒ [2]:
from ultralytics import YOLO
# import os
# os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
model = YOLO("yolo11s.pt")
model.train(data="D:\ASTC project\Pothole-2-3\data.yaml",epochs = 20 ,imgsz= 640, batch =8,workers =4 ,name = "yolos_final")
InΒ [15]:
inference_small_model = YOLO(r"D:\ASTC project\runs\detect\yolos_final4\weights\best.pt")
InΒ [24]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_small_model.predict(image_path ,save_dir = "D:\ASTC project\small_model_1_POthole2-3",conf = 0.25 ,save=True,device= "cuda")
InΒ [25]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict2"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [Β ]:
from ultralytics import YOLO
model = YOLO("yolo11m.pt")
model.train(data="D:\ASTC project\Pothole-2-3\data.yaml",epochs = 15,imgsz= 640, batch = 8,workers =4 ,name = "yoloM_final")
InΒ [1]:
from ultralytics import YOLO
inference_medium_model = YOLO(r"D:\ASTC project\runs\detect\yoloM_final3\weights\best.pt")
InΒ [35]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_medium_model.predict(image_path ,save_dir = "D:\ASTC project\nano_model_1_POthole2-3",conf = 0.25 ,save=True,device= "cuda")
InΒ [36]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict3"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [6]:
import torch, gc
gc.collect()
torch.cuda.empty_cache()
InΒ [2]:
# Training on dataset New pothole detection-2
from ultralytics import YOLO
model = YOLO("yolo11n.pt")
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =40 , verbose = False ,imgsz = 640 , batch = 16 ,name = "New_yolon_1",workers=4)
InΒ [52]:
inference_NEW_nano_model = YOLO(r"D:\ASTC project\runs\detect\New_yolon_1\weights\best.pt")
InΒ [53]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_NEW_nano_model.predict(image_path ,save_dir = "D:\ASTC project\small_model_1_POthole2-3",conf = 0.30 ,save=True,device= "cuda")
InΒ [54]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict4"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [3]:
# Training on dataset New pothole detection-2
from ultralytics import YOLO
model = YOLO("yolo11s.pt")
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =30 , verbose = False ,imgsz = 640 , batch = 16 ,name = "New_yolos_1",workers=4)
InΒ [55]:
inference_NEW_small_model = YOLO(r"D:\ASTC project\runs\detect\New_yolos_1\weights\best.pt")
InΒ [56]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_NEW_small_model.predict(image_path ,save_dir = "D:\ASTC project\small_model_1_POthole2-3",conf = 0.30 ,save=True,device= "cuda")
InΒ [57]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict5"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [7]:
# Training on dataset New pothole detection-2
from ultralytics import YOLO
model = YOLO("yolo11m.pt")
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =20 ,imgsz = 512 , batch = 4 ,name = "New_yoloM_1",workers=8)
InΒ [3]:
from ultralytics import YOLO
inference_NEW_medium_model = YOLO(r"D:\ASTC project\runs\detect\New_yoloM_16\weights\best.pt")
InΒ [4]:
import os
Test_data = r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_NEW_medium_model.predict(image_path ,save_dir = "D:\ASTC project\nano_model_1_POthole2-3",conf = 0.25 ,save=True,device= "cuda")
InΒ [60]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict6"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
Training the existing model on both datasets [FINAL MODEL WITH BOTH DATSETS]ΒΆ
InΒ [4]:
# Training nano model of Pothole2-3 and training it again on New pothole detection-2
from ultralytics import YOLO
model = inference_nano_model
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =40 ,imgsz = 640 , batch = 16 ,name = "yoloN_final_combined",workers=4)
InΒ [3]:
from ultralytics import YOLO
inference_Combined_nano_model= YOLO(r"D:\ASTC project\runs\detect\yoloN_final_combined\weights\best.pt")
InΒ [7]:
import os
Test_data= r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_Combined_nano_model.predict(image_path ,save_dir = "D:\ASTC project\Combined_nano_model_1_POthole2-3",conf = 0.25 ,save=True,device= "cuda")
InΒ [8]:
import os
from IPython.display import Image,display
pred_dir = "runs/detect/predict7"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [12]:
import os
Test_data = r"D:\ASTC project\Test_data_1_real"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_Combined_nano_model.predict(image_path ,save_dir = "D:\ASTC project\Combined_nano_model_1_POthole2-3",conf = 0.10 ,save=True,device= "cuda")
InΒ [13]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict8"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
Small model on both datasetΒΆ
InΒ [16]:
# Training small model of Pothole2-3 and training it again on New pothole detection-2
from ultralytics import YOLO
model = inference_small_model
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =20 ,imgsz = 640 , batch = 16 ,name = "yoloS_final_combined",workers=4)
InΒ [2]:
from ultralytics import YOLO
inference_Combined_small_model= YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
InΒ [19]:
import os
Test_data= r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_Combined_small_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [20]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict_small_combined"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [21]:
import os
Test_data = r"D:\ASTC project\Test_data_1_real"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_Combined_small_model.predict(image_path ,save_dir = "D:\ASTC project\Combined_nano_model_1_POthole2-3",conf = 0.10 ,save=True,device= "cuda")
InΒ [2]:
# Training medium model of Pothole2-3 and training it again on New pothole detection-2
from ultralytics import YOLO
model = inference_medium_model
model.train(data = r"D:\ASTC project\New-pothole-detection-2\data.yaml" ,epochs =20 ,imgsz = 512 , batch = 4 ,name = "yoloM_final_combined",workers=4)
InΒ [3]:
from ultralytics import YOLO
inference_Combined_medium_model= YOLO(r"D:\ASTC project\runs\detect\yoloM_final_combined4\weights\best.pt")
InΒ [5]:
import os
Test_data= r"D:\ASTC project\Data_to_be_predicted"
for files in os.listdir(Test_data):
image_path = os.path.join(Test_data,files)
result = inference_Combined_medium_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [6]:
import os
from IPython.display import Image, display
pred_dir = "runs/detect/predict_medium_combined"
for i in range(len(os.listdir(pred_dir))):
print("Displaying output for image:", i+1)
output_img = os.listdir(pred_dir)[i]
display(Image(filename=os.path.join(pred_dir, output_img)))
boxes = result[0].boxes
print("Potholes detected:", len(boxes))
InΒ [12]:
Test_Pothole_2_3 = r"D:\ASTC project\Pothole-2-3\test\images"
for files in os.listdir(Test_Pothole_2_3):
image_path = os.path.join(Test_Pothole_2_3,files)
result = inference_Combined_nano_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [15]:
import os
Test_Pothole_2_3 = r"D:\ASTC project\Pothole-2-3\test\images"
for files in os.listdir(Test_Pothole_2_3):
image_path = os.path.join(Test_Pothole_2_3,files)
result = inference_Combined_small_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [13]:
import os
Test_New_pothole = r"D:\ASTC project\New-pothole-detection-2\test\images"
for files in os.listdir(Test_New_pothole):
image_path = os.path.join(Test_New_pothole,files)
result = inference_Combined_small_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [14]:
import os
Test_New_pothole = r"D:\ASTC project\New-pothole-detection-2\test\images"
for files in os.listdir(Test_New_pothole):
image_path = os.path.join(Test_New_pothole,files)
result = inference_Combined_nano_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [5]:
import os
import shutil
label = r"D:\ASTC project\Dataset 3\Data\labels"
images =r"D:\ASTC project\Dataset 3\Data\images"
folder_name = r"D:\ASTC project\Dataset 3\Data"
for filename in os.listdir(folder_name):
if filename.endswith(".txt"):
shutil.move(os.path.join(folder_name, filename), os.path.join(label, filename))
if filename.endswith(".jpg"):
shutil.move(os.path.join(folder_name, filename), os.path.join(images, filename))
InΒ [8]:
import os
Five_weather_data = r"D:\ASTC project\Dataset 3\Data\images"
for files in os.listdir(Five_weather_data):
image_path = os.path.join(Five_weather_data,files)
result = inference_Combined_nano_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [11]:
import os
Five_weather_data = r"D:\ASTC project\Dataset 3\Data\images"
for files in os.listdir(Five_weather_data):
image_path = os.path.join(Five_weather_data,files)
result = inference_Combined_small_model.predict(image_path ,conf = 0.25 ,save=True,device= "cuda")
InΒ [2]:
import os
import shutil
import random
source_dir=r"D:\ASTC project\runs\detect\predict_nano_combine_on_pothole2-3"
Destination_dir=r"D:\ASTC project\Final results\Nano_combine_on_pothole2-3"
num_images = 100
all_images = [f for f in os.listdir(source_dir) if f.lower().endswith(('.jpg'))]
selected_images = random.sample(all_images, min(num_images, len(all_images)))
for img in selected_images:
shutil.copy(os.path.join(source_dir, img), os.path.join(Destination_dir, img))
InΒ [2]:
import os
import shutil
import random
source_dir=r"D:\ASTC project\runs\detect\predict_five_weather_small_combined"
Destination_dir=r"D:\ASTC project\Final results\Small_five_weather_result"
num_images = 100
all_images = [f for f in os.listdir(source_dir) if f.lower().endswith(('.jpg'))]
selected_images = random.sample(all_images, min(num_images, len(all_images)))
for img in selected_images:
shutil.copy(os.path.join(source_dir, img), os.path.join(Destination_dir, img))
InΒ [Β ]:
import os
import random
import shutil
dataset_path = r"D:\ASTC project\Dataset 3\Data"
images_path = os.path.join(dataset_path, "images")
labels_path = os.path.join(dataset_path, "labels")
# Output directories
output_dir = r"D:\ASTC project\dataset3_splitted"
for split in ["train", "val", "test"]:
os.makedirs(os.path.join(output_dir, split, "images"), exist_ok=True)
os.makedirs(os.path.join(output_dir, split, "labels"), exist_ok=True)
# Get all images
all_images = [f for f in os.listdir(images_path) if f.endswith((".jpg"))]
# random.shuffle(all_images)
# Split ratios
train_ratio = 0.7
val_ratio = 0.2
test_ratio = 0.1
train_split = int(len(all_images) * train_ratio)
val_split = int(len(all_images) * (train_ratio + val_ratio))
train_files = all_images[:train_split]
val_files = all_images[train_split:val_split]
test_files = all_images[val_split:]
def copy_files(file_list, split):
for file in file_list:
src_img = os.path.join(images_path, file)
dst_img = os.path.join(output_dir, split, "images", file)
shutil.copy(src_img, dst_img)
label_file = os.path.splitext(file)[0] + ".txt"
src_label = os.path.join(labels_path, label_file)
if os.path.exists(src_label):
dst_label = os.path.join(output_dir, split, "labels", label_file)
shutil.copy(src_label, dst_label)
copy_files(train_files, "train")
copy_files(val_files, "val")
copy_files(test_files, "test")
InΒ [14]:
from ultralytics import YOLO
model = inference_Combined_nano_model
model.train(data = r"D:\ASTC project\dataset3_splitted\data.yaml" ,epochs =20 ,imgsz = 640 , batch = 16 ,name = "yoloN_combined_five_weather",workers=4)
InΒ [3]:
from ultralytics import YOLO
model = inference_Combined_small_model
model.train(data = r"D:\ASTC project\dataset3_splitted\data.yaml" ,epochs =20 ,imgsz = 640 , batch = 16 ,name = "yoloS _combined_five_weather",workers=4)
InΒ [15]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(
source="D:\ASTC project\input_video1.mp4",
save=False,
save_txt=False,
conf=0.10,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [4]:
from ultralytics import YOLO
inference_Combined_five_weather_small_model= YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
InΒ [Β ]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloN_combined_five_weather11\weights\best.pt")
results = model.predict(
source="D:\ASTC project\input_video1.mp4",
save=False,
conf=0.10,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [Β ]:
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source="D:\ASTC project\input_video1.mp4",
save=True,
conf=0.10,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [Β ]:
model =YOLO(r"D:\ASTC project\runs\detect\yoloN_final_combined\weights\best.pt")
results = model.predict(
source="D:\ASTC project\input_video1.mp4",
save=False,
conf=0.10,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [13]:
import yt_dlp
url = "https://youtu.be/R2Vr3R_Dpj0"
output_path = "videos/my_video.mp4"
ydl_opts = {
'format': 'mp4',
'outtmpl': output_path
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print("Download completed! File saved in:", output_path)
InΒ [12]:
!pip install yt_dlp
InΒ [2]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\my_video.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [5]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Clear_weather"
video_name = r"clear_weather_input_2.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 20, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [12]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\clear_weather_input_2.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [8]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Clear_weather"
video_name = r"clear_weather_input_3.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 10, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [10]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\clear_weather_input_3.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [14]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\clear_weather_input_video(30fps).mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [1]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Night"
video_name = r"night_input.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 30, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [2]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\night_input.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [3]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Rain"
video_name = r"Rain_input.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 30, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [4]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\Rain_input.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [1]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Rain"
video_name = r"Rain_input_20fps.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 20, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [2]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\Rain_input_20fps.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [3]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Night"
video_name = r"night_input_20fps.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 30, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [4]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
results = model.predict(
source=r"D:\ASTC project\night_input_20fps.mp4",
save=True,
conf=0.20,
show=True
)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [Β ]:
# Annotate the dataset3 splitted training images
import cv2
import os
# Using opencv and yolo dimensions for annotation
# Paths
images_folder = r"D:\ASTC project\dataset3_splitted\train\images"
labels_folder = r"D:\ASTC project\dataset3_splitted\train\labels"
output_folder = r"annotated_images_Dataset3_train"
os.makedirs(output_folder, exist_ok=True)
# Loop through images
for img_file in os.listdir(images_folder):
if not img_file.lower().endswith((".jpg", ".png", ".jpeg")):
continue
img_path = os.path.join(images_folder, img_file)
label_path = os.path.join(labels_folder, os.path.splitext(img_file)[0] + ".txt")
img = cv2.imread(img_path)
if img is None:
print(f"{img_file} not found")
continue
h, w, _ = img.shape
with open(label_path, "r") as f:
for line in f:
parts = line.strip().split()
if len(parts) > 5:
continue
class_id, x_center, y_center, bw, bh = map(float, line.split())
class_id = int(class_id)
xmin = int((x_center - bw / 2) * w)
xmax = int((x_center + bw / 2) * w)
ymin = int((y_center - bh / 2) * h)
ymax = int((y_center + bh / 2) * h)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
cv2.putText(img,"",(xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# saving each file into output folder
out_path = os.path.join(output_folder, img_file)
cv2.imwrite(out_path, img)
API data collectionΒΆ
InΒ [11]:
import cv2
url = "http://100.91.160.114:8080" # replace with your phone's IP given by app
cap = cv2.VideoCapture(url)
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow("Phone Live Feed", frame)
# press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
InΒ [Β ]:
import requests
import os
bbox = "77.0,28.4,77.5,28.9"
url = f"https://api.openaerialmap.org/meta?bbox={bbox}"
response = requests.get(url).json()
os.makedirs("oam_data", exist_ok=True)
for idx, result in enumerate(response.get("results", [])):
title = result.get("title", "No title")
files = result.get("files", [])
if files:
download_url = files[0]["url"]
file_ext = download_url.split(".")[-1]
filename = f"oam_data/image_{idx}.{file_ext}"
print(f"Downloading: {title}")
img_data = requests.get(download_url).content
with open(filename, "wb") as f:
f.write(img_data)
print(f"Saved as {filename}\n")
InΒ [7]:
import requests
import os
# bounding box (Delhi example: Connaught Place)
bbox = "77.2080,28.6130,77.2150,28.6180"
url = f"https://api.openaerialmap.org/meta?bbox={bbox}"
response = requests.get(url)
data = response.json()
if "results" in data and data["results"]:
for i, result in enumerate(data["results"], start=1):
if "files" in result and result["files"]:
for file in result["files"]:
download_url = file["url"]
if download_url.endswith(".tif"): # Only GeoTIFF
print(f"Downloading Image {i}: {download_url}")
img_data = requests.get(download_url).content
filename = f"drone_image_{result['uuid']}.tif"
with open(filename, "wb") as f:
f.write(img_data)
print(f"β
Saved {filename}")
else:
print("β οΈ No imagery found for this bounding box.")
InΒ [6]:
import requests
import os
def download_oam_data(bbox, output_dir="oam_images"):
"""Download drone/satellite imagery from OAM for given bbox"""
url = f"https://api.openaerialmap.org/meta?bbox={bbox}"
print(f"π Querying OAM: {url}")
response = requests.get(url)
if response.status_code != 200:
print(f"β OAM API error: {response.status_code}")
print(response.text[:200])
return
data = response.json()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if "results" in data and data["results"]:
print(f"β
Found {len(data['results'])} imagery records.")
for i, result in enumerate(data["results"], start=1):
if "files" in result and result["files"]:
for file in result["files"]:
download_url = file["url"]
if download_url.endswith(".tif"): # Only GeoTIFF
print(f"π₯ Downloading Image {i}: {download_url}")
img_data = requests.get(download_url).content
filename = os.path.join(output_dir, f"{result['uuid']}.tif")
with open(filename, "wb") as f:
f.write(img_data)
print(f"β
Saved {filename}")
else:
print("β οΈ No imagery found for this bounding box.")
if __name__ == "__main__":
# Example: Nepal earthquake area (OAM has data here)
bbox = "85.312,27.685,85.323,27.695"
download_oam_data(bbox)
Continue model predicitingΒΆ
InΒ [3]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\night_input_20fps.mp4"
, conf=0.25, show=True, save=True)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [4]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\Rain_input.mp4"
, conf=0.25, show=True, save=True)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [1]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\Clear_weather_input_video(30fps).mp4"
, conf=0.25, show=True, save=True)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [2]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Sunset"
video_name = r"Sunset_input_30fps.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 30, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [3]:
import cv2
import os
image_folder = r"D:\ASTC project\Dataset 3\Evening"
video_name = r"Evening_input_30fps.mp4"
images = [img for img in os.listdir(image_folder) if img.endswith((".png", ".jpg"))]
images.sort() # ensure frames are in order
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 30, (width, height))
# Write frames
for image in images:
img_path = os.path.join(image_folder, image)
video.write(cv2.imread(img_path))
video.release()
InΒ [4]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\Sunset_input_30fps.mp4"
, conf=0.25, show=True, save=True)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [5]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\Evening_input_30fps.mp4"
, conf=0.25, show=True, save=True)
total_potholes = 0
for i, result in enumerate(results):
classes = result.boxes.cls.tolist()
potholes_in_frame = classes.count(0)
total_potholes += potholes_in_frame
print(f"Total potholes detected in video: {total_potholes}")
InΒ [Β ]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\input_video1.mp4"
, conf=0.15, show=True, save=True)
# Predict 10 and predict 7
InΒ [9]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\my_video.mp4"
, conf=0.10, show=True, save=True)
#Predict 8 and predict 9
checking the live feed into model using camo studioΒΆ
InΒ [1]:
import cv2
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
cap = cv2.VideoCapture(0)
# using camo studio for live stream feed from phone
# using cv.capture(0) for laptop webcam
# using cv.capture(1) for external webcam or camo studio camera
if not cap.isOpened():
print("Cannot access camera")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
# Get frame size & FPS for saving
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30 # fallback if FPS=0
# Define VideoWriter (save as MP4)
out = cv2.VideoWriter(
"pothole_detection_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'), # codec
fps,
(frame_width, frame_height)
)
results = model(frame, conf=0.25)
out.write(annotated_frame)
annotated_frame = results[0].plot()
for box in results[0].boxes:
cls_id = int(box.cls[0])
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
cv2.imshow("YOLOv11 Pothole Detection", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
InΒ [3]:
import cv2
from ultralytics import YOLO
# Load model
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# Open camera (0 = laptop cam, 1 = external/camo studio)
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print("Cannot access camera")
exit()
# Get frame size & FPS for saving (do this once)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30 # fallback if FPS=0
# Define VideoWriter once
out = cv2.VideoWriter(
"pothole_detection_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(frame_width, frame_height)
)
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
# Run YOLO
results = model(frame, conf=0.25)
# Annotate frame
annotated_frame = results[0].plot()
# Save annotated frame
out.write(annotated_frame)
# Print detections
for box in results[0].boxes:
cls_id = int(box.cls[0])
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
# Show live feed
cv2.imshow("YOLOv11 Pothole Detection", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
InΒ [8]:
import cv2
from ultralytics import YOLO
# Load YOLO model
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# Open camera (0=laptop webcam, 1=external, 2+=Camo virtual cam)
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Cannot access camera")
exit()
# Get frame size & FPS for saving
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS)) # fallback if FPS=0
# Define VideoWriter (save as MP4)
out = cv2.VideoWriter(
"pothole_detection_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'), # codec
fps,
(frame_width, frame_height)
)
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
# Save frame to output file
out.write(annotated_frame)
# Show live
cv2.imshow("YOLOv11 Pothole Detection", annotated_frame)
# Print detections
for box in results[0].boxes:
cls_id = int(box.cls[0])
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release() # save file properly
cv2.destroyAllWindows()
InΒ [1]:
import cv2
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
cap = cv2.VideoCapture(1)
# using camo studio for live stream feed from phone
# using cv.capture(0) for laptop webcam
# using cv.capture(1) for external webcam or camo studio camera
if not cap.isOpened():
print("Cannot access camera")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
for box in results[0].boxes:
cls_id = int(box.cls[0])
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
cv2.imshow("YOLOv11 Pothole Detection", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
InΒ [9]:
import cv2
from ultralytics import YOLO
# Load YOLO model
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# Open webcam (0 for laptop cam, 1 for external, 2 for Camo Studio, etc.)
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
# Get video properties
frame_width = int(cap.get(3)) # width
frame_height = int(cap.get(4)) # height
fps = int(cap.get(cv2.CAP_PROP_FPS)) # default 30 if FPS not available
# Define the codec and create VideoWriter object
out = cv2.VideoWriter(
"output.avi", cv2.VideoWriter_fourcc(*'XVID'), fps, (frame_width, frame_height)
)
while True:
ret, frame = cap.read()
if not ret:
break
# Run YOLO detection
results = model(frame, stream=True)
# Draw results on frame
for r in results:
annotated_frame = r.plot()
# Save frame to output video
out.write(annotated_frame)
# Show the frame
cv2.imshow("YOLO Detection", annotated_frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
out.release()
cv2.destroyAllWindows()
InΒ [11]:
from ultralytics import YOLO
model =YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
results = model.predict(source=r"D:\ASTC project\dataset3_splitted\train\images\Vid_Ca_night_00000098.jpg"
, conf=0.15, show=True, save=True)
InΒ [1]:
!pip install mss
Array technique for input processingΒΆ
InΒ [Β ]:
import cv2
import time
from ultralytics import YOLO
# Load YOLO model
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# Load input video
cap = cv2.VideoCapture("input_video1.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Total frames in video: {frame_count}")
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
output_fps = 5
# Define VideoWriter for saving
out = cv2.VideoWriter(
"pothole_detection_slow.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
current_frame = 0
while current_frame < frame_count:
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
start_time = time.time()
# Run YOLO
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
# Save frame to video
out.write(annotated_frame)
# Show frame live
cv2.imshow("Simulated Live Stream", annotated_frame)
# Print detections
for box in results[0].boxes:
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
# Measure time taken
elapsed_time_ms = int((time.time() - start_time) * 1000)
print(f"Processing time: {elapsed_time_ms} ms")
# Jump ahead by elapsed_time_ms frames
current_frame += elapsed_time_ms
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
print("Video saved as pothole_detection_slow.mp4")
InΒ [Β ]:
import cv2
import time
from ultralytics import YOLO
# Load YOLO model
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# Load input video
cap = cv2.VideoCapture("my_video.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Total frames in video: {frame_count}")
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
output_fps = 5 # Try 5 FPS (normal video ~30 FPS)
# Define VideoWriter for saving
out = cv2.VideoWriter(
"my_video_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
current_frame = 0
while current_frame < frame_count:
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
start_time = time.time()
# Run YOLO
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
# Save frame to video
out.write(annotated_frame)
# Show frame live
cv2.imshow("Simulated Live Stream", annotated_frame)
# Print detections
for box in results[0].boxes:
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
# Measure time taken
elapsed_time_ms = int((time.time() - start_time) * 1000)
print(f"Processing time: {elapsed_time_ms} ms")
# Jump ahead by elapsed_time_ms frames
current_frame += elapsed_time_ms
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
print("Video saved as pothole_detection_slow.mp4")
training again on same dataset with fine tuning to avoid overfittingΒΆ
InΒ [2]:
from ultralytics import YOLO
model = YOLO(r"D:\ASTC project\runs\detect\yoloS _combined_five_weather\weights\best.pt")
model.train(
data=r"D:\ASTC project\dataset3_splitted\data.yaml",
epochs=30,
imgsz=640,
batch=16,
lr0=0.01,
optimizer="AdamW",
weight_decay=0.0005,
name = "NEw model 19_09_2024",
device=0
)
InΒ [5]:
import cv2
import time
from ultralytics import YOLO
# Load YOLO model
model = YOLO(r"D:\ASTC project\runs\detect\NEw model 19_09_2024\weights\best.pt")
# Load input video
cap = cv2.VideoCapture("my_video.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Total frames in video: {frame_count}")
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
output_fps = 20 # Try 5 FPS (normal video ~30 FPS)
# Define VideoWriter for saving
out = cv2.VideoWriter(
"my_video_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
current_frame = 0
while current_frame < frame_count:
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
start_time = time.time()
# Run YOLO
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
# Save frame to video
out.write(annotated_frame)
# Show frame live
cv2.imshow("Simulated Live Stream", annotated_frame)
# Print detections
for box in results[0].boxes:
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
# Measure time taken
elapsed_time_ms = int((time.time() - start_time) * 1000)
print(f"Processing time: {elapsed_time_ms} ms")
# Jump ahead by elapsed_time_ms frames
current_frame += elapsed_time_ms
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
print("Video saved as pothole_detection_slow.mp4")
InΒ [9]:
from ultralytics import YOLO
import os
import numpy as np
import cv2
model =YOLO(r"D:\ASTC project\runs\detect\NEw model 19_09_2024\weights\best.pt")
def predict_image(img_path):
results = model.predict(source=img_path, conf=0.10, show=True, save=True)
if results and results[0].boxes:
class_id = int(results[0].boxes.cls[0])
return class_id
return None
IMG_SIZE = (256, 256) # Adjust based on your model's expected input size
def predict_video(video_path):
cap = cv2.VideoCapture(video_path)
predictions = []
frame_no = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_no += 1
resized = cv2.resize(frame, IMG_SIZE) / 255.0
img_array = np.expand_dims(resized, axis=0)
pred = model.predict(img_array)
predictions.append((frame_no, np.argmax(pred)))
cap.release()
return predictions
# results = model.predict(source=r"D:\ASTC project\recorded_dataset\image1.jpg"
# , conf=0.10, show=True, save=True)
#Predict 8 and predict 9
folder = r"D:\ASTC project\recorded_dataset"
for file in os.listdir(folder):
path = os.path.join(folder, file)
if file.lower().endswith((".jpg", ".png", ".jpeg")):
pred = predict_image(path)
print(f"Image {file} β Prediction: {pred}")
elif file.lower().endswith((".mp4", ".avi", ".mov")):
preds = predict_video(path)
print(f"Video {file} β Frame-wise predictions: {preds[:10]} ...")
InΒ [2]:
from ultralytics import YOLO
import os
model =YOLO(r"D:\ASTC project\runs\detect\NEw model 19_09_2024\weights\best.pt")
folder = r"D:\ASTC project\recorded_dataset"
for file in os.listdir(folder):
path = os.path.join(folder, file)
if file.lower().endswith((".mp4", ".avi", ".mov")):
preds = model.predict(source=path
, conf=0.25, show=True, save=True)
print(f"Video {file} β Frame-wise predictions: {preds[:10]} ...")
Google earthΒΆ
InΒ [10]:
import ee
ee.Authenticate()
ee.Initialize()
# Load Sentinel-2 imagery
collection = ee.ImageCollection('COPERNICUS/S2_SR') \
.filterDate('2023-01-01', '2023-12-31') \
.filterBounds(ee.Geometry.Point(77.1025, 28.7041)) # e.g., Delhi
# Median composite
image = collection.median()
# Export region as TIFF
region = ee.Geometry.Rectangle([77.0, 28.5, 77.3, 28.8])
task = ee.batch.Export.image.toDrive(
image=image.clip(region),
description='sentinel_export',
fileFormat='GeoTIFF',
region=region.getInfo()['coordinates'],
scale=10
)
task.start()
InΒ [4]:
!pip install ee
InΒ [8]:
!pip uninstall earthengine-api -y
!pip install earthengine-api==0.1.396
video to images on rainwater datasetΒΆ
InΒ [12]:
import cv2
import os
# -------- SETTINGS --------
video_dir = r"D:\ASTC project\try data" # folder containing input videos
output_dir = r"D:\pothole_images" # folder to save extracted frames
frame_interval = 10 # extract one frame every N frames
# Create output directory if not exists
os.makedirs(output_dir, exist_ok=True)
# Loop through all videos in the folder
for filename in os.listdir(video_dir):
if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
video_path = os.path.join(video_dir, filename)
video_name = os.path.splitext(filename)[0]
video_output_dir = os.path.join(output_dir, video_name)
os.makedirs(video_output_dir, exist_ok=True)
print(f"Processing: {filename}")
cap = cv2.VideoCapture(video_path)
frame_count = 0
saved_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
# Save every Nth frame
if frame_count % frame_interval == 0:
frame_filename = f"frame_{saved_count:05d}.jpg"
frame_path = os.path.join(video_output_dir, frame_filename)
cv2.imwrite(frame_path, frame)
saved_count += 1
frame_count += 1
cap.release()
print(f"β
{saved_count} frames saved from {filename}\n")
print("All videos processed successfully!")
InΒ [13]:
!pip install label-studio label-studio-ml
label-studio start
InΒ [Β ]:
label-studio start
!pip install label-studio label-studio-ml
InΒ [15]:
pip install labelImg
InΒ [2]:
from ultralytics import YOLO
# Load your previously trained YOLOv11 segmentation model
model = YOLO(r"D:\ASTC project\runs\detect\NEw model 19_09_2024\weights\best.pt")
# Start re-training (fine-tuning) on the new Roboflow dataset
model.train(
data=r"D:\ASTC project\annotated data by own rainy video 7 recorded dataset\data.yaml", # new dataset YAML file
epochs=15, # you can increase if needed
imgsz=640, # input image size
batch=4, # batch size (adjust if you get CUDA OOM)
name="New_model_2",
device="cuda", # or "cpu" if no GPU
lr0=0.001, # optional: learning rate for fine-tuning
patience=10, # early stop if no improvement
)
InΒ [1]:
from ultralytics import YOLO
import os
model =YOLO(r"D:\ASTC project\runs\detect\NEw model 19_09_2024\weights\best.pt")
folder = r"D:\ASTC project\recorded_dataset"
for file in os.listdir(folder):
path = os.path.join(folder, file)
if file.lower().endswith((".mp4", ".avi", ".mov")):
preds = model.predict(source=path
, conf=0.25, show=True, save=True)
# print(f"Video {file} β Frame-wise predictions: {preds[:10]} ...")
InΒ [Β ]:
import cv2
# Open USB webcam (usually index 1 or higher; try 1 for USB cam)
cap = cv2.VideoCapture(2)
if not cap.isOpened():
print("Cannot access USB webcam")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
cv2.imshow("USB Webcam Feed", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Array technique part 2ΒΆ
InΒ [2]:
import cv2
import time
from ultralytics import YOLO
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
cap = cv2.VideoCapture("input_video1.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f"Total frames in video: {frame_count}")
output_fps = 20
out = cv2.VideoWriter(
"pothole_detection_timed_20fps.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
current_frame = 0
while current_frame < frame_count:
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
start_time = time.time()
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
cv2.imshow("Real-Time Simulated Output", annotated_frame)
out.write(annotated_frame)
for box in results[0].boxes:
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
elapsed_time_ms = int((time.time() - start_time) * 1000)
print(f"Frame {current_frame} processing time: {elapsed_time_ms} ms")
key = cv2.waitKey(elapsed_time_ms if elapsed_time_ms > 1 else 1)
if key & 0xFF == ord('q'):
print("Interrupted by user.")
break
current_frame += 1
cap.release()
out.release()
cv2.destroyAllWindows()
print("Video saved as pothole_detection_timed.mp4")
InΒ [Β ]:
import cv2
import time
from ultralytics import YOLO
# -------------------- Load YOLO model --------------------
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# -------------------- Load input video --------------------
cap = cv2.VideoCapture("input_video1.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f"Total frames in video: {frame_count}")
# Output video writer
output_fps = 5
out = cv2.VideoWriter(
"pothole_detection_half_frames.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
# -------------------- Frame Processing --------------------
current_frame = 0
frame_skip = 2 # Process every 2nd frame (half the frames)
while current_frame < frame_count:
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
start_time = time.time()
# Run YOLO detection
results = model(frame, conf=0.25)
annotated_frame = results[0].plot()
# Show annotated frame
cv2.imshow("Simulated Output (Half Frames)", annotated_frame)
out.write(annotated_frame)
# Print detection info
for box in results[0].boxes:
conf = float(box.conf[0])
x1, y1, x2, y2 = box.xyxy[0].tolist()
print(f"Pothole detected: Conf={conf:.2f}, BBox=({x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f})")
# Measure processing time
elapsed_time_ms = int((time.time() - start_time) * 1000)
print(f"Frame {current_frame} processing time: {elapsed_time_ms} ms")
# Wait to simulate real-time output
key = cv2.waitKey(elapsed_time_ms if elapsed_time_ms > 1 else 1)
if key & 0xFF == ord('q'):
print("Interrupted by user.")
break
# Move to next frame (skip frames)
current_frame += frame_skip
# -------------------- Cleanup --------------------
cap.release()
out.release()
cv2.destroyAllWindows()
print("Video saved as 'pothole_detection_half_frames.mp4'")
Array technique for fps 30 yolo frame rateΒΆ
InΒ [Β ]:
import cv2
from ultralytics import YOLO
# -------------------- LOAD MODEL --------------------
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# -------------------- OPEN INPUT VIDEO --------------------
cap = cv2.VideoCapture(r"D:\ASTC project\input_video1.mp4")
if not cap.isOpened():
print("Error: Cannot open video")
exit()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# -------------------- PREPARE OUTPUT VIDEO --------------------
output_fps = 30 # can adjust, or later compute dynamically
out = cv2.VideoWriter(
"pothole_detected_1_30.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
frame_index = 0
detected_frames = 0
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, conf=0.25)
# Only save frame if at least one object detected
if len(results[0].boxes) > 0:
annotated = results[0].plot()
out.write(annotated)
detected_frames += 1
cv2.imshow("Detected Frame", annotated)
else:
cv2.imshow("Detected Frame", frame) # optional, show normal frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frame_index += 1
cap.release()
out.release()
cv2.destroyAllWindows()
print(f"Total frames processed: {frame_index}")
print(f"Total frames saved with detections: {detected_frames}")
print("β
Video saved as pothole_detected_only.mp4")
InΒ [Β ]:
import cv2
from ultralytics import YOLO
# -------------------- LOAD MODEL --------------------
model = YOLO(r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt")
# -------------------- OPEN INPUT VIDEO --------------------
input_path = r"D:\ASTC project\input_video1.mp4"
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
print("β Error: Cannot open video")
exit()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
input_fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
input_duration = total_frames / input_fps # seconds
print(f"π¬ Input duration: {input_duration:.2f} sec at {input_fps:.2f} FPS")
# -------------------- FIRST PASS: COLLECT DETECTED FRAMES --------------------
detected_frames = []
detected_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, conf=0.25, verbose=False)
if len(results[0].boxes) > 0:
annotated = results[0].plot()
detected_frames.append(annotated)
detected_count += 1
cap.release()
if detected_count == 0:
print("β οΈ No detections found β no output video generated.")
exit()
# -------------------- COMPUTE OUTPUT FPS TO MATCH ORIGINAL DURATION --------------------
output_fps = detected_count / input_duration # ensures same 39s playback
print(f"π― Computed Output FPS: {output_fps:.2f} (detected {detected_count} frames)")
# -------------------- SAVE OUTPUT VIDEO --------------------
out_path = "pothole_detected_same_duration.mp4"
out = cv2.VideoWriter(
out_path,
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
for frame in detected_frames:
out.write(frame)
cv2.imshow("Detected Output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.release()
cv2.destroyAllWindows()
print("\nβ
Processing complete!")
print(f"π§© Total frames processed: {total_frames}")
print(f"π¦ Frames with detections: {detected_count}")
print(f"π₯ Output saved as: {out_path} (β{input_duration:.1f} sec)")
# Output -- same duration which is correct
InΒ [Β ]:
import cv2
from ultralytics import YOLO
import os
# -------------------- PATHS --------------------
input_path = r"D:\ASTC project\input_video1.mp4"
converted_input_path = r"D:\ASTC project\input_video_20fps_converted.mp4"
output_path = r"D:\ASTC project\pothole_detected_same_duration_20fps.mp4"
model_path = r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt"
# -------------------- LOAD MODEL --------------------
model = YOLO(model_path)
# -------------------- CONVERT INPUT TO 20 FPS --------------------
print("ποΈ Converting input video to 20 FPS...")
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
print("β Error: Cannot open video")
exit()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
input_fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
input_duration = total_frames / input_fps
print(f"πΉ Original FPS: {input_fps:.2f}, Duration: {input_duration:.2f} sec")
# Create 20 FPS version of input video
target_fps = 20
out_20fps = cv2.VideoWriter(
converted_input_path,
cv2.VideoWriter_fourcc(*'mp4v'),
target_fps,
(frame_width, frame_height)
)
# Keep every nth frame to reach ~20 FPS
frame_skip_ratio = int(round(input_fps / target_fps))
frame_index = 0
while True:
ret, frame = cap.read()
if not ret:
break
if frame_index % frame_skip_ratio == 0:
out_20fps.write(frame)
frame_index += 1
cap.release()
out_20fps.release()
print(f"β
Saved converted 20 FPS video: {converted_input_path}")
# -------------------- LOAD 20 FPS VIDEO FOR DETECTION --------------------
cap = cv2.VideoCapture(converted_input_path)
if not cap.isOpened():
print("β Error: Cannot open converted video")
exit()
new_fps = cap.get(cv2.CAP_PROP_FPS)
new_total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
new_duration = new_total_frames / new_fps
print(f"π¬ Converted video duration: {new_duration:.2f} sec at {new_fps:.2f} FPS")
# -------------------- FIRST PASS: DETECT POTHOLES --------------------
detected_frames = []
detected_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, conf=0.25, verbose=False)
if len(results[0].boxes) > 0:
annotated = results[0].plot()
detected_frames.append(annotated)
detected_count += 1
cap.release()
if detected_count == 0:
print("β οΈ No detections found β no output video generated.")
exit()
# -------------------- ADJUST OUTPUT FPS --------------------
output_fps = detected_count / new_duration # Keeps same duration
print(f"π― Computed Output FPS: {output_fps:.2f} for {detected_count} detected frames")
# -------------------- SAVE DETECTION VIDEO --------------------
out = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
for frame in detected_frames:
out.write(frame)
cv2.imshow("Detected Output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.release()
cv2.destroyAllWindows()
# -------------------- SUMMARY --------------------
print("\nβ
Processing Complete!")
print(f"π§© Total frames (original): {total_frames}")
print(f"ποΈ Converted 20 FPS frames: {new_total_frames}")
print(f"π¦ Frames with detections: {detected_count}")
print(f"β±οΈ Duration matched: {new_duration:.2f} sec")
print(f"π₯ Output video saved: {output_path}")
print(f"πΎ Converted input (20 FPS) saved: {converted_input_path}")
InΒ [Β ]:
import cv2
from ultralytics import YOLO
import math
import os
# -------------------- PATHS --------------------
input_path = r"D:\ASTC project\input_video1.mp4"
converted_input_path = r"D:\ASTC project\input_video_20fps_converted.mp4"
output_path = r"D:\ASTC project\pothole_detected_same_duration_20fps.mp4"
model_path = r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt"
# -------------------- LOAD YOLO MODEL --------------------
model = YOLO(model_path)
# -------------------- STEP 1: CONVERT INPUT TO 20 FPS --------------------
print("ποΈ Converting input video to 20 FPS...")
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
print("β Error: Cannot open input video")
exit()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
input_fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
input_duration = total_frames / input_fps
target_fps = 20
frame_skip_ratio = max(1, math.floor(input_fps / target_fps))
out_20fps = cv2.VideoWriter(
converted_input_path,
cv2.VideoWriter_fourcc(*'mp4v'),
target_fps,
(frame_width, frame_height)
)
frame_index = 0
while True:
ret, frame = cap.read()
if not ret:
break
if frame_index % frame_skip_ratio == 0:
out_20fps.write(frame)
frame_index += 1
cap.release()
out_20fps.release()
print(f"β
Converted video saved at 20 FPS: {converted_input_path}")
# -------------------- STEP 2: LOAD CONVERTED VIDEO FOR DETECTION --------------------
cap = cv2.VideoCapture(converted_input_path)
if not cap.isOpened():
print("β Error: Cannot open converted video")
exit()
new_fps = cap.get(cv2.CAP_PROP_FPS)
new_total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
new_duration = new_total_frames / new_fps
print(f"π¬ Converted video duration: {new_duration:.2f} s at {new_fps:.2f} FPS")
# -------------------- STEP 3: DETECT POTHOLES --------------------
detected_frames = []
detected_count = 0
print("π Detecting potholes...")
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, conf=0.25, verbose=False)
if len(results[0].boxes) > 0:
annotated = results[0].plot()
detected_frames.append(annotated)
detected_count += 1
cap.release()
if detected_count == 0:
print("β οΈ No detections found β output video not generated.")
exit()
print(f"β
Total detected frames: {detected_count}")
# -------------------- STEP 4: ADJUST OUTPUT FPS TO MATCH DURATION --------------------
output_fps = detected_count / new_duration # ensures same total duration
print(f"π― Output FPS adjusted to: {output_fps:.2f} to match duration {new_duration:.2f}s")
# -------------------- STEP 5: SAVE OUTPUT VIDEO --------------------
out = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*'mp4v'),
output_fps,
(frame_width, frame_height)
)
for frame in detected_frames:
out.write(frame)
cv2.imshow("Detected Output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.release()
cv2.destroyAllWindows()
# -------------------- STEP 6: SUMMARY --------------------
print("\nβ
Processing Complete!")
print(f"πΉ Original FPS: {input_fps:.2f}, Frames: {total_frames}, Duration: {input_duration:.2f}s")
print(f"ποΈ Converted (20 FPS): {new_total_frames} frames, Duration: {new_duration:.2f}s")
print(f"π¦ Frames with detections: {detected_count}")
print(f"β±οΈ Output duration β {new_duration:.2f}s (auto-adjusted FPS)")
print(f"π₯ Output video saved β {output_path}")
print(f"πΎ 20 FPS input saved β {converted_input_path}")
# Correct output code for array technique new results 20 fps
# converting input video to 20 fps and output sa 20 fps too
Downloading annotaetd images of rainy datasetΒΆ
InΒ [Β ]:
import cv2
import os
# Using opencv and yolo dimensions for annotation
# Paths
images_folder = r"D:\ASTC project\annotated data by own rainy video 7 recorded dataset\train\images"
labels_folder = r"D:\ASTC project\annotated data by own rainy video 7 recorded dataset\train\labels"
output_folder = r"D:\ASTC project\annotated data by own rainy video 7 recorded dataset\annotated images by own"
os.makedirs(output_folder, exist_ok=True)
# Loop through images
for img_file in os.listdir(images_folder):
if not img_file.lower().endswith((".jpg", ".png", ".jpeg")):
continue
img_path = os.path.join(images_folder, img_file)
label_path = os.path.join(labels_folder, os.path.splitext(img_file)[0] + ".txt")
img = cv2.imread(img_path)
if img is None:
print(f"{img_file} not found")
continue
h, w, _ = img.shape
with open(label_path, "r") as f:
for line in f:
parts = line.strip().split()
if len(parts) > 5:
continue
class_id, x_center, y_center, bw, bh = map(float, line.split())
class_id = int(class_id)
xmin = int((x_center - bw / 2) * w)
xmax = int((x_center + bw / 2) * w)
ymin = int((y_center - bh / 2) * h)
ymax = int((y_center + bh / 2) * h)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
cv2.putText(img,"",(xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# saving each file into output folder
out_path = os.path.join(output_folder, img_file)
cv2.imwrite(out_path, img)
InΒ [Β ]:
import cv2
from ultralytics import YOLO
import math
import os
import numpy as np
input_path = r"D:\ASTC project\input_video1.mp4"
converted_input_path = r"D:\ASTC project\input_video_20fps_converted_same duration.mp4"
output_path = r"D:\ASTC project\pothole_detected_same_duration_20fps_new_1.mp4"
model_path = r"D:\ASTC project\runs\detect\yoloS_final_combined\weights\best.pt"
model = YOLO(model_path)
print("ποΈ Converting input video to 20 FPS...")
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
print("β Error: Cannot open input video")
exit()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
input_fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
input_duration = total_frames / input_fps
target_fps = 20
total_target_frames = int(input_duration * target_fps)
frame_interval = input_duration / total_target_frames
out_20fps = cv2.VideoWriter(
converted_input_path,
cv2.VideoWriter_fourcc(*'mp4v'),
target_fps,
(frame_width, frame_height)
)
frame_times = np.linspace(0, input_duration, total_target_frames)
for t in frame_times:
cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000)
ret, frame = cap.read()
if not ret:
break
out_20fps.write(frame)
cap.release()
out_20fps.release()
print(f"β
Converted to 20 FPS (same duration): {converted_input_path}")